﻿#include "UICheckbox.h"

UICheckbox::UICheckbox()
{
	SetupEventListeners();
}

UICheckbox::UICheckbox(float xPos, float yPos) : UIElement(xPos, yPos)
{
	SetupEventListeners();
}

UICheckbox::UICheckbox(float xPos, float yPos, float size) : UIElement(xPos, yPos, size, size)
{
	SetupEventListeners();
}

UICheckbox::UICheckbox(float xPos, float yPos, float size, std::string text) : UIElement(xPos, yPos, size, size), IUITextElement(text)
{
	SetupEventListeners();
}

UICheckbox::UICheckbox(float xPos, float yPos, float size, std::string text, std::string fontName) : UIElement(xPos, yPos, size, size), IUITextElement(text, fontName)
{
	SetupEventListeners();
}

UICheckbox::UICheckbox(float xPos, float yPos, float size, std::string text, std::string fontName, int fontSize) : UIElement(xPos, yPos, size, size), IUITextElement(text, fontName, fontSize)
{
	SetupEventListeners();
}

UICheckbox::UICheckbox(float xPos, float yPos, float size, std::string text, std::string fontName, int fontSize, float margins) : UIElement(xPos, yPos, size, size), IUITextElement(text, fontName, fontSize)
{
	SetupEventListeners();
	Margins = margins;
}

void __stdcall UICheckbox::Draw(Graphics* graphics)
{
	// Drawing the checkbox area
	if (!this->RoundedCorners)
		graphics->DrawRectangle(Position.x, Position.y, Size, Size, color.r, color.g, color.b, color.a, 0, true);
	else
		graphics->DrawRoundedRectangle(Position.x, Position.y, Size, Size, RoundCornerRadiusX, RoundCornerRadiusY, color.r, color.g, color.b, color.a, 0, true);

	// If checkbox is checked, draw the checkmark
	if (Checked)
	{
		graphics->drawText(L"✔", "Verdana", CheckmarkFontSize, FONT_STYLE_NORMAL, FONT_WEIGHT_NORMAL, Position.x + 0.2f, Position.y + 0.2f, Size - 0.2f, Size - 0.2f,
			CheckmarkColor.r, CheckmarkColor.g, CheckmarkColor.b, CheckmarkColor.a, DWRITE_TEXT_ALIGNMENT_CENTER, DWRITE_PARAGRAPH_ALIGNMENT_CENTER);
	}

	// Drawing the text after the checkbox
	graphics->drawText(Text, FontName, FontSize, FontStyle, FontWeight, Position.x + Size + Margins + 1, Position.y, Text.size() * (float)FontSize, Size,
		TextColor.r, TextColor.g, TextColor.b, TextColor.a, DWRITE_TEXT_ALIGNMENT_LEADING, DWRITE_PARAGRAPH_ALIGNMENT_CENTER);
}

void UICheckbox::SetupEventListeners()
{
	AddLeftMouseClickEvent([this](UIElement* sender) {
		Checked = !Checked;
		for (callback_function cb : m_CheckedStateChangedEvents)
			cb(this);
	});
}
